package com.zygh.lz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.dao.SignMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.vo.TrafficPoliceDutyOverviewVO;
import com.zygh.lz.service.TrafficPoliceDutyOverviewService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.util.JwtUtils;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.StatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @program: zygh-transform
 * @description: 交警勤务概况
 * @author: jhaol
 * @created: 2023/05/26 11:28
 */
@Slf4j
@Service
public class TrafficPoliceDutyOverviewServiceImpl implements TrafficPoliceDutyOverviewService {
    @Resource
    private SignMapper signMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private JwtUtils jwtUtils;

    @Override
    public ResultBean<TrafficPoliceDutyOverviewVO> getTrafficPoliceDutyOverview(HttpServletRequest request) {
        // 不同角色查看不同内容
        Integer sectionDid = getSectionId(request);

        // 累计签到
        int totalPoliceNum = signMapper.policeSignCount(sectionDid, null);
        // 实时在线
        int policeOnline = signMapper.policeSignCount(sectionDid, 1);
        List<Map<String, Object>> totalPostNumTemp = signMapper.selectSignPost(sectionDid, null);
        List<Map<String, Object>> postOnlineNumTemp = signMapper.selectSignPost(sectionDid, 1);

        int postTotalNum = signMapper.countSingPostNum(sectionDid, null);
        Map<String, Integer> totalPostNum = new HashMap<>();
        for (Map<String, Object> map : totalPostNumTemp) {
            totalPostNum.put(map.get("post_name").toString(), Integer.valueOf(map.get("count").toString()));
        }
        int postOnlineCount = signMapper.countSingPostNum(sectionDid, 1);

        Map<String, Integer> postOnlineNum = new HashMap<>();
        for (Map<String, Object> map : postOnlineNumTemp) {
            postOnlineNum.put(map.get("post_name").toString(), Integer.valueOf(map.get("count").toString()));
        }


        TrafficPoliceDutyOverviewVO trafficPoliceDutyOverviewVO = new TrafficPoliceDutyOverviewVO();

        // 总警力
        trafficPoliceDutyOverviewVO.setPoliceTotalNum(totalPoliceNum);
        trafficPoliceDutyOverviewVO.setPoliceOnlineNum(policeOnline);
        // 岗位
        trafficPoliceDutyOverviewVO.setPostTotalNum(postTotalNum);
        trafficPoliceDutyOverviewVO.setPostOnlineNum(postOnlineCount);

        // 带班领导
        Integer brigadeLeaderTotal = totalPostNum.get("带班大队领导");
        Integer detachmentLeaderTotal = totalPostNum.get("带班支队领导");
        if (brigadeLeaderTotal == null) {
            brigadeLeaderTotal = 0;
        }
        if (detachmentLeaderTotal == null) {
            detachmentLeaderTotal = 0;
        }

        Integer brigadeLeaderOnline = postOnlineNum.get("带班大队领导");
        Integer detachmentLeaderOnline = postOnlineNum.get("带班支队领导");
        if (brigadeLeaderOnline == null) {
            brigadeLeaderOnline = 0;
        }
        if (detachmentLeaderOnline == null) {
            detachmentLeaderOnline = 0;
        }


        trafficPoliceDutyOverviewVO.setLeaderTotalNum(brigadeLeaderTotal + detachmentLeaderTotal);
        trafficPoliceDutyOverviewVO.setLeaderOnlineNum(brigadeLeaderOnline + detachmentLeaderOnline);
        // 铁骑
        trafficPoliceDutyOverviewVO.setIronRideTotalNum(totalPostNum.get("铁骑") != null ? totalPostNum.get("铁骑") : 0);
        trafficPoliceDutyOverviewVO.setIronRideOnlineNum(postOnlineNum.get("铁骑") != null ? postOnlineNum.get("铁骑") : 0);

        // 网格
        trafficPoliceDutyOverviewVO.setGridPostTotalNum(totalPostNum.get("网格") != null ? totalPostNum.get("网格") : 0);
        trafficPoliceDutyOverviewVO.setGridPostOnlineNum(postOnlineNum.get("网格") != null ? postOnlineNum.get("网格") : 0);
        // 机动岗
        trafficPoliceDutyOverviewVO.setMobilePostTotalNum(totalPostNum.get("机动岗") != null ? totalPostNum.get("机动岗") : 0);
        trafficPoliceDutyOverviewVO.setMobilePostOnlineNum(postOnlineNum.get("机动岗") != null ? postOnlineNum.get("机动岗") : 0);
        // 高速岗
        trafficPoliceDutyOverviewVO.setHighSpeedPostTotalNum(totalPostNum.get("高速岗") != null ? totalPostNum.get("高速岗") : 0);
        trafficPoliceDutyOverviewVO.setHighSpeedPostOnlineNum(postOnlineNum.get("高速岗") != null ? postOnlineNum.get("高速岗") : 0);
        // 固定岗
        trafficPoliceDutyOverviewVO.setFixedPostTotalNum(totalPostNum.get("固定岗") != null ? totalPostNum.get("固定岗") : 0);
        trafficPoliceDutyOverviewVO.setFixedPostOnlineNum(postOnlineNum.get("固定岗") != null ? postOnlineNum.get("固定岗") : 0);
        // 夜巡
        trafficPoliceDutyOverviewVO.setNightWatchPostTotalNum(totalPostNum.get("夜巡") != null ? totalPostNum.get("夜巡") : 0);
        trafficPoliceDutyOverviewVO.setNightWatchPostOnlineNum(postOnlineNum.get("夜巡") != null ? postOnlineNum.get("夜巡") : 0);

        // 特勤
        int specialPostTotalNum = signMapper.countSpecialSignPost(sectionDid, null);
        int specialPostOnlineNum = signMapper.countSpecialSignPost(sectionDid, 1);
        trafficPoliceDutyOverviewVO.setSpecialPostTotalNum(specialPostTotalNum);
        trafficPoliceDutyOverviewVO.setSpecialPostOnlineNum(specialPostOnlineNum);

        return ResultUtil.setOK("查询成功", trafficPoliceDutyOverviewVO);
    }

    private Integer getSectionId(HttpServletRequest request) {
        String token = request.getHeader(jwtUtils.getHeader());

        String userName = jwtUtils.getClaimByToken(token).getSubject();
        Staff staff = staffMapper.selectOne(Wrappers.<Staff>lambdaQuery().eq(Staff::getStaffTel, userName));
        Integer sectionDid = null;
        if (Objects.nonNull(staff)) {
            sectionDid = staff.getSysSectionId();
            if (staff.getSysSectionId() == 74) {
                sectionDid = null;
            }
        }
        return sectionDid;
    }
}
